package org.wattdepot.cli.command;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.wattdepot.cli.processor.ChartCommand;
import org.wattdepot.cli.processor.CommandLineInterface;

/**
 * Test for the Chart command.
 * 
 * @author Kendyll Doi
 * @author Kimberly Heu
 * 
 */
public class TestChartCommand extends CommandLineInterface {

  /**
   * Tests the Chart methods.
   * 
   * @throws Exception If problems occur.
   */
  @Test
  public void testChartCommand() throws Exception {

    List<String> commandList = new ArrayList<String>(); // List of commands

    commandList.add("chart");
    commandList.add("power");
    commandList.add("generated");
    commandList.add("SIM_WAIAU");
    commandList.add("2009-11-15");
    commandList.add("2009-11-16");
    commandList.add("sampling-interval");
    commandList.add("60");
    commandList.add("file");
    commandList.add("test.html");

    ChartCommand command = new ChartCommand(); // command

    // The result from executing the individual method
    String methodResult =
        WattChart.wattChart("generated", "SIM_WAIAU", "2009-11-15", "2009-11-16", 60, "test.html",
            client);
    // The result from executing corresponding command
    String commandResult = command.doCommand(commandList);

    // Ensure answer is correct
    assertTrue("Checking method result", methodResult.contains("File Created"));
    // Ensure both results are correct
    assertEquals("Checking method and command results are equal", methodResult, commandResult);
  }

  /**
   * Test that the input to the command is validated.
   * 
   * @throws Exception if problem occurs
   */
  @Test
  public void testInputValidation() throws Exception {

    List<String> commandList = new ArrayList<String>();

    commandList.add("chart");
    commandList.add("power");
    commandList.add("generated");
    commandList.add("randomsource");
    commandList.add("2009-11-17");
    commandList.add("2009-11-18");
    commandList.add("sampling-interval");
    commandList.add("60");
    commandList.add("file");
    commandList.add("test.html");

    ChartCommand command = new ChartCommand();
    String commandResult = command.doCommand(commandList);

 //    Test for an incorrect source
    System.out.println(commandResult);
    assertTrue("Checking invalid source", commandResult.startsWith("No data found"));

    commandList.remove(3);
    commandList.add(3, "SIM_WAIAU");
    
    commandList.remove(4);
    commandList.add(4, "09-14-15");

    // Test for invalid use of "timestamp" argument
    commandResult = command.doCommand(commandList);
    assertTrue("Checking invalid format of command", commandResult.contains("not correct"));

    // Test for starttime > endtime
    commandList.remove(4);
    commandList.add(4, "2009-11-17");
    commandList.remove(5);
    commandList.add(5, "2009-11-14");
    commandResult = command.doCommand(commandList);
    assertTrue("Checking invalid dates in commands", commandResult.contains("End date cannot"));

    //Test for error in type
    commandList.remove(4);
    commandList.add(4, "2009-11-12");
    commandList.remove(5);
    commandList.add(5, "2009-11-13");
    commandList.remove(2);
    commandList.add(2, "neither");
    commandResult = command.doCommand(commandList);
    assertTrue("Checking invalid entry of commands", commandResult.contains("Error"));
    
    // Test for 0 interval
    commandList.remove(7);
    commandList.add(7, "0");
    commandResult = command.doCommand(commandList);
    assertTrue("Checking invalid interval in commands", commandResult
        .contains("interval must be greater than 0"));

    // Test for too few arguments
    commandList.remove(9);
    commandResult = command.doCommand(commandList);
    assertTrue("Checking invalid number of commands", commandResult.contains("Invalid input"));    

  }
}
